package com.jz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jz.mapper.auto.TCollectionMapper;
import com.jz.module.dto.TCollectionDto;
import com.jz.module.dto.base.PageInfoDto;
import com.jz.module.entity.TCollection;
import com.jz.module.entity.TCollectionExample;
import com.jz.service.CollectionService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    @SuppressWarnings("all")
    private TCollectionMapper tCollectionMapper;

    @Override
    public ResultUtil page(TCollectionDto dto) {
        PageHelper.startPage(dto.getPageNo(), dto.getPageSize());
        TCollectionExample example = new TCollectionExample();
        example.setOrderByClause("create_time desc");
        List<TCollection> tCollections = tCollectionMapper.selectByExample(example);
        PageInfo<TCollection> info = new PageInfo<>(tCollections);
        PageInfoDto<TCollectionDto> result = new PageInfoDto<TCollectionDto>();
        BeanUtils.copyProperties(info, result);
        return ResultUtil.success(result);
    }


    @Override
    public ResultUtil query(TCollectionDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        TCollection tCollection = tCollectionMapper.selectByPrimaryKey(dto.getId());
        if (tCollection == null) {
            return ResultUtil.error(SysCode.NO_DATA);
        }
        TCollectionDto result = new TCollectionDto();
        BeanUtils.copyProperties(tCollection, result);
        return ResultUtil.success(result);
    }

    @Override
    public ResultUtil save(TCollectionDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getMessageId(),
                dto.getStudentId()
        )) {
            return ResultUtil.paramError();
        }

        TCollectionExample example = new TCollectionExample();
        example.createCriteria()
                .andMessageIdEqualTo(dto.getMessageId())
                .andStudentIdEqualTo(dto.getStudentId());
        List<TCollection> selectByExample = tCollectionMapper.selectByExample(example);
        if (selectByExample != null && selectByExample.size() > 0) {
            return ResultUtil.error("不可重复收藏");
        }
        // insert
        TCollection insert = new TCollection();
        BeanUtils.copyProperties(dto, insert);

        insert.setId(UUIDUtil.getUUID());
        insert.setCreateTime(new Date());
        int res = tCollectionMapper.insert(insert);
        if (res < 1) {
            return ResultUtil.error(SysCode.INSERT_ERROR);
        }

        return ResultUtil.success();
    }

    @Override
    public ResultUtil delete(TCollectionDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getId())) {
            return ResultUtil.paramError();
        }
        int i = tCollectionMapper.deleteByPrimaryKey(dto.getId());
        if (i > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error(SysCode.DELETE_ERROR);
    }
}
